﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table=FCNS.Data.Table;
using DoubleH.Utility.Configuration;
using System.IO.Ports;

namespace POS
{
    /// <summary>
    /// WindowConfig.xaml 的交互逻辑
    /// </summary>
    public partial class WindowConfig : Window
    {
        public WindowConfig()
        {
            InitializeComponent();

            InitVar();
            InitEvent();
        }

        public void Init()
        {
            comboBoxMode.Text = DoubleHConfig.AppConfig.PosMode;
            checkBoxFullScreen.IsChecked = DoubleHConfig.AppConfig.PosFullScreen;
            colorPickerBackground.SelectedColor = (Color)ColorConverter.ConvertFromString(DoubleHConfig.AppConfig.PosBackgroundColor);
            colorPickerForeground.SelectedColor = (Color)ColorConverter.ConvertFromString(DoubleHConfig.AppConfig.PosForegroundColor);
            FontSize.Value = DoubleHConfig.AppConfig.PosFontSize;
            checkBoxShowImg.IsChecked = DoubleHConfig.AppConfig.PosShowImg;

            checkBoxPrint.IsChecked = DoubleHConfig.AppConfig.PosIsPrint;
            checkBoxPrintJiaoBan.IsChecked = DoubleHConfig.AppConfig.PosIsPrintJiaoBan;
            checkBoxShowCashboxMoney.IsChecked = DoubleHConfig.AppConfig.ShowCashboxMoney;

            comboBoxLed.Text = DoubleHConfig.AppConfig.PosLed;
        }

        private void InitVar()
        {
            comboBoxMode.Items.Add("便利店");
            comboBoxMode.Items.Add("外卖店(触摸屏)");

            foreach (string str in DoubleHConfig.AppConfig.PosJiaoBan.Split(';'))
            {
                if (string.IsNullOrEmpty(str))
                    continue;

                listBoxJiaoBan.Items.Add(str);
            }

            comboBoxLed.Items.Add("无");
            foreach (string s in SerialPort.GetPortNames())
                comboBoxLed.Items.Add(s);
        }

        private void InitEvent()
        {
            listBoxJiaoBan.MouseDoubleClick += (ss, ee) => { RemoveJiaoBanTime(); };
            buttonAdd.Click += (ss, ee) => { AddJiaoBanTime(); };
            buttonPosNO.Click += (ss, ee) => { ShowPosNO(); };
            buttonSave.Click += (ss, ee) => { Save(); };
        }

        private void AddJiaoBanTime()
        {
            string time = timePickerJiaoBan.Value.Value.ToString("HHmm");
            if (!listBoxJiaoBan.Items.Contains(time))
                listBoxJiaoBan.Items.Add(time);
        }

        private void RemoveJiaoBanTime()
        {
            if (listBoxJiaoBan.SelectedItem == null)
                return;

            listBoxJiaoBan.Items.Remove(listBoxJiaoBan.SelectedItem);
        }

        private void ShowPosNO()
        {
            if (Table.UserS.LoginUser.GetAuthority(DataTableText.Pos机器号)>Table.UserS.EnumAuthority.编辑)
            {
                PosNOconfig p = new PosNOconfig(Table.PosS.GetObject(DoubleHConfig.AppConfig.PosNO));
                p.ShowDialog();
            }
            else
                MessageWindow.Show("权限不足");
        }

        private void Save()
        {
            DoubleHConfig.AppConfig.PosMode = comboBoxMode.Text;
            DoubleHConfig.AppConfig.PosFullScreen = checkBoxFullScreen.IsChecked.Value;
            DoubleHConfig.AppConfig.PosBackgroundColor = colorPickerBackground.SelectedColorText;
            DoubleHConfig.AppConfig.PosForegroundColor = colorPickerForeground.SelectedColorText;
            DoubleHConfig.AppConfig.PosFontSize = FontSize.Value.Value;
            DoubleHConfig.AppConfig.PosShowImg = checkBoxShowImg.IsChecked.Value;
            DoubleHConfig.AppConfig.PosLed = comboBoxLed.Text;

            DoubleHConfig.AppConfig.PosIsPrint = checkBoxPrint.IsChecked.Value;
            DoubleHConfig.AppConfig.PosIsPrintJiaoBan = checkBoxPrintJiaoBan.IsChecked.Value;
            DoubleHConfig.AppConfig.ShowCashboxMoney = checkBoxShowCashboxMoney.IsChecked.Value;

            StringBuilder sb = new StringBuilder();
            foreach (object obj in listBoxJiaoBan.Items)
                sb.Append(obj + ";");

            DoubleHConfig.AppConfig.PosJiaoBan = sb.ToString();
            this.Close();
        }
    }
}
